import sys
import pygame
import pygame
pygame.init()

screen = pygame.display.set_mode((512,480))#画布
background=pygame.image.load('.\\background.png')#导入图片
run=pygame.image.load('.\\spritesheets\\run.png').convert_alpha()
idle=pygame.image.load('.\\spritesheets\\idle.png').convert_alpha()


pygame.mixer.music.load(".\\bgm\\nature sketch.wav")  # 加载音乐
pygame.mixer.music.set_volume(0.5)# 设置音量大小0~1的浮点数
pygame.mixer.music.play() # 播放音频


run_rect=run.get_rect()
idle_rect=idle.get_rect()

screen.blit(run, (10,10))

clock = pygame.time.Clock()#创建时钟对象
x=0#玩家x坐标,y坐标,移动速度v
y=128
v=12
start_x=0
start_x2=0
while True:#死循环让窗口一直显示
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # if event.type == pygame.KEYDOWN:  # 如果检测到按键按下
        #     if event.key == pygame.K_d:  # 如果键是D键
        #         print("D键被按下!")
    clock.tick(8)

    start_x%=256
    screen.blit(background, (0, 0))
    start_x2%=192
    screen.blit(idle,(x,y),area=(start_x2,0,32,64))
    start_x2 += 32
    # 如果事件类型为 KEYDOWN，即按下键盘按钮，则检查事件键
    # if event.type == pygame.KEYDOWN:
    #     pass
    # 如果按下的按钮是右箭头键，则增加 x 坐标
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_d]:
        # 如果右键被按下dd
        print("D key is pressed")
        x+=13
        start_x += 32
        screen.blit(idle, (x, y), area=(0, 0, 0, 0))
        screen.blit(run, (x, y), area=(start_x, 0, 32, 64))

    else:start_x=0
    if keys_pressed[pygame.K_a]:
        # 如果右键被按下dd
        print("A key is pressed")
        #pygame.transform.flip(fum_rect, 1, 0)

    pygame.display.flip()



